<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class Antz_TagFilter_Config
{
    protected $configs = array(
        'attributeWhitelist' => array(),
        'attributeBlacklist' => array(),
        'tagnameWhitelist' => array(),
        'tagnameWhitelist' => array(),
        'explicitWhitelist' => array(),
        'explicitBlacklist' => array(),
        'htmlMode' => 'xhtml',
        'errors' => array(),
        'removeNodes' => array(),
        'allowDoctype' => false
    );

    public function __construct(){

    }

    public function set($configName, $configValue){
        if(array_key_exists($configName, $this->configs)){
            if(is_array($this->configs[$configName]) && is_array($configValue) && count($configValue)>0){
                $this->configs[$configName] = array_merge($this->configs[$configName], $configValue);
            }else{
                $this->configs[$configName] = $configValue;
            }
        }
    }

    public function get($who=null){
        if($who===null){
            return $this->configs;
        }
        if(isset($this->configs[$who])){
            return $this->configs[$who];
        }
    }

    public function __call($methodName, $parameters){

        switch($methodName){
            case 'allowAttributes':
                $configName = 'attributeWhitelist';
            break;
            case 'denyAttributes':
                $configName = 'attributeBlacklist';
            break;
            case 'allowTags':
                $configName = 'tagnameWhitelist';
            break;
            case 'denyTags':
                $configName = 'tagnameBlacklist';
            break;
            case 'allowExplicit':
                $configName = 'explicitWhitelist';
            break;
            case 'denyExplicit':
                $configName = 'explicitBlacklist';
            break;

        }
        switch($methodName){
            case 'allowAttributes':
            case 'denyAttributes':
            case 'allowTags':
            case 'denyTags':
                foreach($parameters as $parameter){
                    if(is_array($parameter)){
                        foreach($parameter as $k=>$v){
                            $this->set($configName, array($v));
                        }
                    }else{
                        $this->set($configName, array($parameter));
                    }
                }
            break;

            case 'allowExplicit':
            case 'denyExplicit':
                if(count($parameters)==1){
                    foreach($parameters as $tagname=>$attname){
                        if(is_array($attname)){
                            foreach($attname as $k=>$v){
                                if(is_array($k)){
                                    $this->set($configName, $k);
                                }else if(is_array($v)){
                                    $this->set($configName, $v);
                                }else{
                                    $this->set($configName, array($k=>$v));
                                }
                            }
                        }else{
                            $this->set($configName, array($tagname=>$attname));
                        }
                    }

                }else if(count($parameters)==2){
                    $this->set($configName, array($parameters[0]=>$parameters[1]));
                }
            break;

        }
    }
}


